package com.bzframework.pro.io.web;

import com.bzframework.pro.authorize.annotation.Authorization;
import com.bzframework.pro.io.manager.FileManager;
import com.bzframework.pro.io.entity.FileInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author bingzhong
 * @Date 2024/12/6 13:42
 * @Description
 */
@Tag(name = "文件管理")
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

    private final FileManager fileManager;

    @PostMapping("/upload")
    @Operation(summary = "单文件上传")
    @Authorization(ignore = true)
    public Mono<FileInfo> fileUpload(@RequestPart FilePart file) {
        return fileManager.saveFile(file);
    }

    @PostMapping("/batch/upload")
    @Operation(summary = "多文件上传")
    @Authorization(ignore = true)
    public Flux<FileInfo> fileUpload(@RequestPart Flux<FilePart> files) {
        return files.flatMap(fileManager::saveFile);
    }

    @GetMapping("/read")
    @Operation(summary = "文件读取")
    @Authorization(ignore = true)
    public Mono<Void> readFile(@RequestParam String fileId, ServerWebExchange webExchange) {
        return fileManager.readFile(fileId, webExchange);
    }


}
